package com.dqyt.khjc.utils;

import com.dqyt.khjc.config.Config;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.awt.AlphaComposite;

import java.awt.Color;

import java.awt.Font;

import java.awt.Graphics2D;

import java.awt.RenderingHints;

import java.awt.Transparency;

import java.awt.image.BufferedImage;

import java.io.*;

import java.util.Date;

import javax.imageio.ImageIO;


public class PNGUtil {


    /**
     * 给图片添加水印文字、可设置水印文字的旋转角度
     *
     */
    @SuppressWarnings("unused")
    public static boolean markImageByText(String SrcBase64,String savePath){

        InputStream is = null;
        OutputStream os = null;
        byte[] b;
        try {
            //String dateString = "大庆油田"; // 需要加入的水印文字 (时间)
            String dateString = "dqytjyzx";
            BASE64Decoder base64de = new BASE64Decoder();
            b = base64de.decodeBuffer(SrcBase64);
            is = new java.io.ByteArrayInputStream(b);
            BufferedImage srcImg = ImageIO.read(is);
            // 1、源图片
            BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null), srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
            // 2、得到画笔对象
            Graphics2D g = buffImg.createGraphics();
            buffImg = g.getDeviceConfiguration().createCompatibleImage(srcImg.getWidth(null), srcImg.getHeight(null), Transparency.TRANSLUCENT);
            g.dispose();
            g = buffImg.createGraphics();
            // 3、设置对线段的锯齿状边缘处理
            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            // 4、把原图画到新画板上
            g.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null), srcImg.getHeight(null), java.awt.Image.SCALE_SMOOTH), 0, 0, null);
            // 5、设置水印文字颜色
            g.setColor(Color.black);
            // 6、设置水印文字Font
            g.setFont(new java.awt.Font("微软雅黑", java.awt.Font.BOLD, 60));
            // 7、设置水印文字透明度 必须要用AlphaComposite.SRC_OVER
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP));
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.3f)); // 1.0f为透明度 ，值从0-1.0，依次变得不透明
            // 8、设置水印旋转
            g.rotate(Math.toRadians(40), (double) buffImg.getWidth(), (double) buffImg.getHeight());
            // 9、第一参数->设置的内容，后面两个参数->文字在图片上的坐标位置(x,y)
            g.drawString(dateString, 50, 460);
            g.drawString(dateString, 230, 300);

           // g.drawString(dateString, 120, 120);
            // 10、释放资源
            g.dispose();
            // 11、生成图片
            os = new FileOutputStream(savePath);
           boolean res = ImageIO.write(buffImg, "png", os);
           return  res;
        }catch (Exception e){
            e.printStackTrace();
            return  false;
        }finally {
            if (null != is) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != os) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    //读取图片转为Base64字符串
    public static String TuImage(String args) {
        String base64Str="";
        String imgFile = args;//待处理的图片
        InputStream in = null;
        byte[] data = null;
        //读取图片字节数组
        try{
            in = new FileInputStream(imgFile);
            data = new byte[in.available()];
            in.read(data);
            in.close();
            //对字节数组Base64编码
            BASE64Encoder encoder = new BASE64Encoder();
            base64Str = encoder.encode(data);
        }catch (IOException e){
            e.printStackTrace();
        }
        return base64Str;
    }
}
